package com.joyintech.modules.workflow.rest;

import com.joyintech.annotation.Log;
import com.joyintech.modules.workflow.service.WorkFlowService;
import com.joyintech.modules.workflow.service.dto.ModelQueryCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * 1. 流程定义控制器
 * @author daiweiwei
 * @date 2018-12-03
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：流程管理->流程定义")
@RequestMapping("/api/workflow/definitions")
public class ProcessDefinitionsController {
    private final WorkFlowService workFlowService;

    @Log("上传流程定义文件")
    @ApiOperation("上传流程定义文件")
    @PostMapping
    @PreAuthorize("@el.check('definitions:upload')")
    public ResponseEntity<Object> upload(@RequestParam("file") MultipartFile file,@RequestParam(required = false,defaultValue = "false") boolean isSyncConvert) throws Exception {
        workFlowService.uploadProcessDefinitions(file,isSyncConvert);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @ApiOperation("流程定义列表")
    @GetMapping
    @PreAuthorize("@el.check('definitions:list')")
    public ResponseEntity<Object> list(ModelQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(workFlowService.listProcessDefinitions(criteria,pageable), HttpStatus.OK);
    }

    @Log("删除流程定义文件")
    @ApiOperation("删除流程定义文件")
    @DeleteMapping
    @PreAuthorize("@el.check('definitions:del')")
    public ResponseEntity<Object> delete(@RequestBody Set<String> ids,
                                         @RequestParam(name = "cascade",required = false,defaultValue = "true") boolean cascade){
        workFlowService.deleteProcessDefinitions(ids,cascade);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("激活或挂起流程定义文件")
    @ApiOperation("激活或挂起流程定义文件")
    @PutMapping("edit")
    @PreAuthorize("@el.check('definitions:edit')")
    public ResponseEntity<Object> edit(String procDefId,String state) throws Exception {
        workFlowService.updateProcessDefinitions(procDefId,state);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @Log("转为模型")
    @ApiOperation("转为模型")
    @PutMapping("convert/{deploymentId}")
    @PreAuthorize("@el.check('definitions:convert')")
    public ResponseEntity<Object> convert(@PathVariable  String deploymentId) throws Exception {
        workFlowService.convertProcessDefinitions(deploymentId);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("查看流程定义图")
    @ApiOperation("查看流程定义图")
    @GetMapping("diagrams/{procDefId}")
   /* @PreAuthorize("@el.check('definitions:diagrams')")*/
    public void diagrams(HttpServletResponse response ,@PathVariable String procDefId) throws Exception{
        workFlowService.showProcessDefinitionsDiagrams(response,procDefId);
    }
}
